package com.jrzh.mvc.controller.sys.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.annotation.NoPermission;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.annotation.UserEvent;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.bean.JqDataGrid;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.search.sys.FileSearch;
import com.jrzh.mvc.search.sys.UserSearch;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.FileView;
import com.jrzh.mvc.view.sys.UserPasswordView;
import com.jrzh.mvc.view.sys.UserView;
@RestController(UserController.LOCATION +"/UserController")
@RequestMapping(UserController.LOCATION)
public class UserController extends BaseAdminController{
	public static final String LOCATION = "sys/admin/user";
	
	@Autowired
	SysServiceManage sysServiceManage;
	
	@PostMapping(value = "datagrid")
	@UserEvent(desc = "管理员列表查询")
	@RequiredPermission(key =  SysActionConstants.INDEX)
	public JqDataGrid<UserView> datagrid(UserSearch search) {
		JqDataGrid<UserView> dg = new JqDataGrid<UserView>();
		try {
			String eqRoleId = request.getParameter("eqRoleId");
			search.setInIds(sysServiceManage.userService.findUserIdsByRoleId(eqRoleId));
			dg = sysServiceManage.userService.datagrid(search);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return dg;
	}
	
	
	@PostMapping(value = "add")
	@UserEvent(desc = "管理员增加")
	@RequiredPermission(key =  SysActionConstants.ADD)
	public ResultBean add(UserView view) {
		ResultBean result = new ResultBean();
		try {
			sysServiceManage.userService.addUser(view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
		}catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	
	@PostMapping(value = "edit/{id}")
	@UserEvent(desc = "管理员修改")
	@RequiredPermission(key =  SysActionConstants.EDIT)
	public ResultBean edit(@PathVariable("id") String id, UserView view) {
		ResultBean result = new ResultBean();
		try {
			sysServiceManage.userService.editUser(id, view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
		}catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "editInfo/{id}")
	@UserEvent(desc = "修改個人信息")
	@NoPermission
	public ResultBean editInfo(@PathVariable("id") String id, UserView view) {
		ResultBean result = new ResultBean();
		try {
			sysServiceManage.userService.editUser(id, view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setObjs(getSessionUser());
		}catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "changeStatus/{id}")
	@UserEvent(desc = "启用/禁用")
	@RequiredPermission(key =  SysActionConstants.DISABLE_ENABLE)
	public ResultBean changeStatus(@PathVariable String id){
		ResultBean result = new ResultBean();
		try {
			result = sysServiceManage.userService.changeStatus(id, getSessionUser());
		} catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "resetPassword/{id}")
	@UserEvent(desc = "重置密码")
	@RequiredPermission(key =  SysActionConstants.RESET_PASSWORD)
	public ResultBean resetPassword(@PathVariable String id){
		ResultBean result = new ResultBean();
		try {
			result = sysServiceManage.userService.resetPassword(id, getSessionUser());
		} catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "load_data/{id}")
	@UserEvent(desc = "读取用户信息")
	@NoPermission
	public ResultBean loadData(@PathVariable("id") String id){
		ResultBean result = new ResultBean();
		try {
			UserView user = sysServiceManage.userService.findViewById(id);
			if(null == user) {
				result.setMsg(I18nHelper.getI18nByKey("message.obtain_user_info_failure", getSessionUser()));
				return result;
			}
			FileSearch fileSearch = new FileSearch();
			fileSearch.setEqKey("admin_user_photo");
			fileSearch.setEqualModel("sys_user");
			fileSearch.setEqualFormId(user.getId());
			List<FileView> files = sysServiceManage.fileService.viewList(fileSearch);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("files", files);
			result.setStatus(ResultBean.SUCCESS);
			result.setObjs(new Object[]{user});
			result.setData(data);
			result.setMsg(I18nHelper.getI18nByKey("message.obtain_user_info_success", getSessionUser()));
		} catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "changePassword/{id}")
	@UserEvent(desc = "修改密碼")
	@NoPermission
	public ResultBean changePassword(@PathVariable("id") String id, UserPasswordView view) {
		ResultBean result = new ResultBean();
		try {
			sysServiceManage.userService.changePassword(id, view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setObjs(getSessionUser());
		}catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	
	@Override
	protected void setData() {
	}
}
